<template>
    <section>
        <!--工具条-->
        <div class="section-content clearfix">
            <el-col :span="24" style="padding-bottom: 0px;">
                <el-form @submit.native.prevent :inline="true" :model="filters">
                    <el-form-item>
                        <el-input v-model="filters.name" placeholder="设备名字" @keyup.enter.native="getUsers()"></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-input v-model="filters.deviceSn" placeholder="SN" @keyup.enter.native="getUsers()">
                        </el-input>
                    </el-form-item>
                    <el-form-item>
                        <!-- <el-form-item> -->
                        <el-button :type="buttonColor" v-on:click="getUsers">查询</el-button>
                    </el-form-item>
                </el-form>
            </el-col>

            <!--列表-->
            <el-table :data="query" class="alarmshistory" highlight-current-row v-loading="listLoading" @selection-change="selsChange" :header-row-style="{backgroundColor: titleColor}"
                style="width: 100%;">

                <el-table-column align="center" type="index" label="序号"  width="80":index="indexMethod">
                </el-table-column>
                <el-table-column align="center" prop="deviceName" label="设备名字">
                </el-table-column>

                <el-table-column align="center" prop="companyName" label="组织">
                </el-table-column>

                <el-table-column align="center" prop="deviceSn" label="网关SN">
                    <template scope="scope">
                        
                        <el-popover
                            placement="top-start"
                            width="200"
                            trigger="hover">
                            <div style="text-align:center;">
                                <qriously v-model="initQCode" :size="150" />
                            </div>
                            <span slot="reference" :type="buttonColor" style="color:#0099cc;cursor: pointer;"
                            @click="eidtsn(scope.row)" @mouseover="qrget(scope.row)">{{scope.row.deviceSn}}</span>
                        </el-popover>
                    </template>
                </el-table-column>

                <el-table-column align="center" prop="modelName" label="型号">
                </el-table-column>



                <el-table-column align="center" prop="iccid" label="ICCID">
                </el-table-column>

                <el-table-column align="center" prop="softVersion" label="固件版本">
                </el-table-column>

                <el-table-column  align="center" label="在线/离线" :formatter="formatstate">
                </el-table-column>
                <el-table-column prop="" align="center" label="模块复位" :formatter="formatstate" width="110">
                    <template scope="scope">

                        <el-button :type="buttonColor" @click="getdata('resetModule',scope.row)">模块复位</el-button>

                    </template>
                </el-table-column>


                <el-table-column label="操作" align="center">
                    <template scope="scope">

                        <span>
                            <i title="一键获取数据" @click="getdata('getAllData',scope.row)" type="warning"
                                class="pb-icon icon-general_order"></i>
                        </span>
                        <span>
                            <i title="设备信息" type="warning" @click="special(scope.$index, scope.row)"
                                class="pb-icon icon-Photoshop"></i>
                        </span>
                        <span>
                            <i title="整机条码" type="warning" @click="getdata('getBarCode', scope.row)"
                                class="pb-icon icon-yuanchengshuju" ></i>
                        </span>
                        <span>
                            <i title="从机版本" type="warning" @click="getdata('getSlaveVersion', scope.row)"
                                class="pb-icon icon-yuanchengkongzhi" ></i>
                        </span>
                   
                        <!-- <el-button :type="buttonColor"  v-on:click="getCode">整机条码</el-button> -->
                    
                    </template>

                </el-table-column>
            </el-table>

            <!--工具条-->
            <el-col :span="24" class="toolbar">
                <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="10"
                    :total="total" style="float:right;">
                </el-pagination>
            </el-col>
        </div>
        <!--编辑界面-->
        <el-dialog title="命令" v-show="editFormVisible" v-model="editFormVisible" @close="editFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form @submit.native.prevent :model="editForm" label-width="80px" :rules="editFormRules"
                    ref="editForm">
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('getdata')" :type="buttonColor">获取数据</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('resetModule')" :type="buttonColor">模块重启</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('timeSync')" :type="buttonColor">执行网络校时</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('connectState')" :type="buttonColor">获取与云服务器的连接状态</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('signalIntensity')" :type="buttonColor">获取模块信号强度</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('getModuleType')" :type="buttonColor">获取模块类型</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('getIMEI')" :type="buttonColor">获取模块IMEI码</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('getICCID')" :type="buttonColor">获取SIM卡ICCID</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('getPhoneNum')" :type="buttonColor">获取SIM卡手机号</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('getPosition')" :type="buttonColor">获取定位信息</el-button>
                    </el-form-item>
                    <el-form-item prop="name" class="grade3">

                        <el-button @click="getdata('getVersion')" :type="buttonColor">获取自身固件版本</el-button>
                    </el-form-item>



                </el-form>

            </div>
        </el-dialog>

        <!--修改界面-->
        <el-dialog title="编辑" :custom-class="dialogTitleColor" v-show="addFormVisible" v-model="addFormVisible" @close="addFormVisible=false">
            <div slot="footer" style="text-align: left; ">
                <el-form @submit.native.prevent :model="editForm" label-width="100px" :rules="addFormRules"
                    ref="editForm">
                    <el-form-item label="网关SN" prop="deviceSn">
                        <el-input v-model="editForm.deviceSn" auto-complete="off"></el-input>
                    </el-form-item>


                    <el-form-item label="密码" prop="password">
                        <el-input v-model="editForm.password" auto-complete="off"></el-input>
                    </el-form-item>

                </el-form>
                <div slot="footer" class="dialog-footer" style="text-align: right;">
                    <el-button @click.native="addFormVisible = false">取消</el-button>
                    <el-button :type="buttonColor" @click.native="editSubmit" :loading="addLoading">提交</el-button>
                </div>
            </div>
        </el-dialog>
    </section>
</template>

<script>
    // import util from './../common/js/util'
    //import NProgress from 'nprogress'
    import {
        device_model_query,
        device_model_post,
        device_model_put,
        device_model_delete,
        device_query,
        device_cmd,
        device_attr_query,
        update_device_sn
    } from '../../api/api';

    export default {
        data() {
            return {
                filters: {
                    name: '',
                    deviceSn: ''
                },
                query: [],
                total: 0,
                page: 1,
                listLoading: false,
                sels: [], //列表选中列
                initQCode:"",
                editFormVisible: false, //编辑界面是否显示
                editLoading: false,
                editFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }]
                },
                //编辑界面数据
                editForm: {
                    deviceId: '',
                    deviceSn: '',
                    password: ''

                },

                addFormVisible: false, //新增界面是否显示
                addLoading: false,
                addFormRules: {
                    name: [{
                        required: true,
                        message: '请输入姓名',
                        trigger: 'blur'
                    },{ min: 1, max: 20, message: '长度在 1 到 20 个字符', trigger: 'blur' }],
                    deviceSn: [{
                            required: true,
                            message: '请输入SN',
                            trigger: 'blur'
                        }

                    ],
                    password: [{
                        required: true,
                        message: '请输入密码',
                        trigger: 'blur'
                    }]
                },
                //新增界面数据
                addForm: {
                    name: '',
                    remark: ''
                }

            }
        },
        computed:{
            buttonColor(){
                return this.$store.state.buttonColor
            },
            titleColor(){
                return this.$store.state.titleColor
            },
            dialogTitleColor(){
                return this.$store.state.dialogTitleColor
            },
        },
        methods: {
            qrget(s) {
                console.log(s)
                this.initVisible = true;
                this.initQCode = s.deviceSn;
            },
            indexMethod(index) {
                let num = (this.page - 1) * 10 + index + 1;
                return num;
            },
            formatstate(row, column) {

                return row.state == 1 ? '在线' : row.state == 0 ? '离线' : '未知';
            },
            special(index, row) {
                sessionStorage.setItem('facility_details', row.deviceId)
                this.$router.push({
                    path: '/devices/gatewaydis'
                })
            },
            getdata(item, row) {

                this.$confirm('确认发送命令吗', '提示', {}).then(() => {
                    let para = {
                        code: item,
                        deviceIdList: [row.deviceId],
                        attrs: {

                        }
                    };
                    // console.log(para)
                    this.loading = true;
                    //NProgress.start();
                    device_cmd(para).then((res) => {
                         this.begCode(res.code,res.message);
                        this.$message({
                            message: '已发送命令',
                            type: 'success'
                        });
                        //NProgress.done();
                    });
                });
            },
            formatSex: function (row, column) {
                return row.sex == 1 ? '男' : row.sex == 0 ? '女' : '未知';
            },
            handleCurrentChange(val) {
                this.page = val;
                this.getUsers();
            },
            eidtsn(s) {
                this.addFormVisible = true;
                this.editForm = {
                    deviceId: s.deviceId,
                    deviceSn: s.deviceSn
                }

            },
            sneidtbt() {
                let para = this.editForm
                //NProgress.start();
                this.addLoading = true;
                update_device_sn(para).then((res) => {
                    this.begCode(res.code, res.message);
                    this.addLoading = false;
                    if (res.code == 0) {
                        this.$message({
                            message: '修改成功',
                            type: 'success'
                        });

                        this.addFormVisible = false;
                        this.getUsers();
                    }
                    //NProgress.done();
                });
            },
            //获取菜单列表
            getUsers() {
                let para = {
                    pageNo: this.page,
                    pageSize: 10,
                    name: this.filters.name,
                    deviceSn: this.filters.deviceSn
                };

                this.listLoading = true;
                //NProgress.start();
                device_attr_query(para).then((res) => {
                     this.begCode(res.code,res.message);
                    this.total = res.data.totalCount;
                    this.query = res.data.list;
                    this.listLoading = false;
                    //NProgress.done();
                });
            },
            //删除
            handleDel: function (index, row) {
                this.$confirm('确认删除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        idList: [row.id]
                    };
                    device_model_delete(para).then((res) => {
                         this.begCode(res.code,res.message);
                        if (res.code == 0) {

                            this.listLoading = false;
                            //NProgress.done();
                            this.$message({
                                message: '删除成功',
                                type: 'success'
                            });
                            this.getUsers();

                        }
                    });
                }).catch(() => {

                });
            },
            //显示编辑界面
            handleEdit: function (index, row) {
                this.editFormVisible = true;
                this.editForm = Object.assign({}, row);
            },
            //显示新增界面
            handleAdd: function () {
                this.addFormVisible = true;
                this.addForm = {
                    name: '',
                    password: ''
                };
            },
            //编辑
            editSubmit: function () {
                this.$refs.editForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            let para = this.editForm
                            //NProgress.start();
                            this.addLoading = true;
                            update_device_sn(para).then((res) => {
                                this.begCode(res.code, res.message);
                                this.addLoading = false;
                                if (res.code == 0) {
                                    this.$message({
                                        message: '修改成功',
                                        type: 'success'
                                    });

                                    this.addFormVisible = false;
                                    this.getUsers();
                                }
                                //NProgress.done();
                            });
                        });
                    }
                });
            },
            //新增
            addSubmit: function () {
                this.$refs.addForm.validate((valid) => {
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            this.addLoading = true;
                            //NProgress.start();
                            let para = Object.assign({}, this.addForm);

                            device_model_post(para).then((res) => {
                                 this.begCode(res.code,res.message);
                                this.addLoading = false;
                                //NProgress.done();
                                this.$message({
                                    message: '提交成功',
                                    type: 'success'
                                });
                                this.$refs['addForm'].resetFields();
                                this.addFormVisible = false;
                                this.getUsers();
                            });
                        });
                    }
                });
            },
            selsChange: function (sels) {
                this.sels = sels;
            },
            //批量删除
            batchRemove: function () {
                var ids = this.sels.map(item => item.id).toString();
                this.$confirm('确认删除选中记录吗？', '提示', {
                    type: 'warning'
                }).then(() => {
                    this.listLoading = true;
                    //NProgress.start();
                    let para = {
                        idList: ids.split(',')
                    };
                    device_model_delete(para).then((res) => {
                         this.begCode(res.code,res.message);
                        this.listLoading = false;
                        //NProgress.done();
                        this.$message({
                            message: '删除成功',
                            type: 'success'
                        });
                        this.getUsers();
                    });
                }).catch(() => {

                });
            },
           
        },
        mounted() {
            this.getUsers();
        }
    }
</script>

<style lang="scss" scoped>

</style>